package com.polaris.live.dialog

import android.content.Context
import android.content.Intent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.cache.CommonCache
import com.polaris.live.databinding.DialogDmaBinding
import com.polaris.live.resp.event.DmaEvent
import com.polaris.live.ui.set.DmaActivity
import com.polaris.live.utils.DmaUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * dma弹窗
 *
 * @author Created by lucas on 2024/2/19 03:42
 * @since 1.0.0
 */
class DmaDialog(
    context: Context,
    private val forceShow: Boolean,
    private val listener: (confirm: Boolean) -> Unit
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_dma
    }

    private lateinit var mBinding: DialogDmaBinding

    override fun onCreate() {
        super.onCreate()

        EventBus.getDefault().register(this)

        mBinding = DialogDmaBinding.bind(popupImplView).apply {
            tvConfirm.setOnClickListener {
                listener(true)

                DmaUtils.setDmaUserSelected(true)

                dismiss()
            }
            tvManager.setOnClickListener {
                val intent = Intent(context, DmaActivity::class.java)
                intent.putExtra("forceShow", forceShow)
                context.startActivity(intent)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onDmaConfirm(event: DmaEvent) {
        listener(event.agree)

        dismiss()
    }

    override fun dismiss() {
        super.dismiss()

        EventBus.getDefault().unregister(this)
    }

    companion object {

        /**
         * 显示dma弹窗
         *
         * @return 是否显示了
         */
        fun checkAndShowDmaDialog(
            context: Context,
            listener: (DmaData) -> Unit = {},
            ipEea: Boolean? = null
        ): Boolean {
            val dmaSelected = DmaUtils.getDmaSelected()
            if (dmaSelected != null) {
                listener(DmaData(true, dmaSelected))
                return false
            }

            val finalIpEea = ipEea ?: CommonCache.getIpCountry()?.let { DmaUtils.isEEA(it) }
            //如果ip不是欧盟，直接配置为true
            if (finalIpEea == false) {
                DmaUtils.setDmaUserSelected(true)
                return false
            }

            //如果是欧盟ip或者ip国家未获取并且本地是欧盟国家
            return (finalIpEea == true || DmaUtils.hasPrivacy()).also { isEea ->
                if (isEea) {
                    DmaUtils.showedDmaDialog()

                    showDmaDialog(context, listener)
                } else {
                    listener(DmaData.other())
                }
            }
        }

        /**
         * 显示dma弹窗
         */
        fun showDmaDialog(
            context: Context,
            listener: (DmaData) -> Unit = {},
            forceShow: Boolean = false,
            isCancel: Boolean = false
        ) {
            XPopup.Builder(context)
                .dismissOnTouchOutside(isCancel)
                .dismissOnBackPressed(isCancel)
                .isDestroyOnDismiss(true)
                .asCustom(DmaDialog(context, forceShow) { listener(DmaData(true, it)) })
                .show()
        }
    }
}

data class DmaData(
    /**
     * 是否为欧盟国家
     */
    val isEea: Boolean,
    /**
     * 是否同意信息收集
     */
    val confirm: Boolean
) {

    companion object {

        /**
         * 其他国家
         */
        fun other(): DmaData {
            return DmaData(false, false)
        }

        /**
         * 欧盟
         */
        fun eea(confirm: Boolean): DmaData {
            return DmaData(true, confirm)
        }
    }
}